package com.goal.plugin;

import com.intellij.openapi.application.ApplicationActivationListener;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.wm.IdeFrame;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.event.KeyEvent;

public class SelectKeyListener implements ApplicationActivationListener {

    private final KeyEventDispatcher keyEventDispatcher = new SelectKeyEventDispatcher();

    @Override
    public void applicationActivated(@NotNull IdeFrame ideFrame) {
        ApplicationActivationListener.super.applicationActivated(ideFrame);
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(keyEventDispatcher);
    }

    @Override
    public void applicationDeactivated(@NotNull IdeFrame ideFrame) {
        ApplicationActivationListener.super.applicationDeactivated(ideFrame);
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.removeKeyEventDispatcher(keyEventDispatcher);
    }

    @Override
    public void delayedApplicationDeactivated(@NotNull Window ideFrame) {
        ApplicationActivationListener.super.delayedApplicationDeactivated(ideFrame);
    }

    private static class SelectKeyEventDispatcher implements KeyEventDispatcher {

        private boolean shiftPressed = false;
        private boolean cltPressed = false;
        private boolean winPressed = false;

        private final EditorSettingsExternalizable  editorSettingsExternalizable =
                EditorSettingsExternalizable.getInstance();

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            int keyCode = e.getKeyCode();
            int id = e.getID();

            if (id == KeyEvent.KEY_PRESSED) {
                switch (keyCode) {
                    case KeyEvent.VK_SHIFT -> shiftPressed = true;
                    case KeyEvent.VK_CONTROL -> cltPressed = true;
                    case KeyEvent.VK_WINDOWS -> winPressed = true;
                }
                // 同时按下
                if (shiftPressed && cltPressed && winPressed) {
                    editorSettingsExternalizable.setCamelWords(true);
                }
            } else if (id == KeyEvent.KEY_RELEASED) {
                switch (keyCode) {
                    case KeyEvent.VK_WINDOWS ->  {
                        if (shiftPressed && cltPressed) {
                            editorSettingsExternalizable.setCamelWords(false);
                        }
                        winPressed = false;
                    }
                    case KeyEvent.VK_SHIFT -> shiftPressed = false;
                    case KeyEvent.VK_CONTROL -> cltPressed = false;
                }
            }
            System.out.println("ctl: " + cltPressed);
            System.out.println("shift: " + shiftPressed);
            System.out.println("win: " + winPressed);
            System.out.println("open statue: " + editorSettingsExternalizable.isCamelWords());
            return true;
        }

    }
}
